<template>
  <el-dialog title="提示" :visible.sync="show" width="600px">
    <div>
      <el-form ref="ruleForm" :rules="rules" label-position="top" label-width="80px" :model="ruleForm">
        <el-form-item prop="school_id" label="院系" required>
          <el-select v-model="ruleForm.school_id" @change="changeSchool">
            <el-option v-for="item in schoolList" :key="item.id" :value="item.id" :label="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item prop="class_id" label="班级" required>
          <el-select v-model="ruleForm.class_id">
            <el-option v-for="item in classList" :key="item.id" :value="item.id" :label="item.class_name" />
          </el-select>
        </el-form-item>
        <el-form-item prop="student_name" label="学生姓名" required>
          <el-input v-model="ruleForm.student_name" max="16" />
        </el-form-item>
        <el-form-item prop="number" label="工号" required>
          <el-input v-model="ruleForm.number" max="16" type="number" />
        </el-form-item>
        <el-form-item prop="sex" label="性别" required>
          <el-select v-model="ruleForm.sex">
            <el-option v-for="item in sexList" :key="item.id" :value="item.id" :label="item.name" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="onAdd">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { mapGetters } from 'vuex'
import { http } from '@/api'

export default {
  name: 'PageAdd',
  data() {
    return {
      show: false,
      ruleForm: {},
      rules: {},
      currentItem: {},
      sexList: [
        { id: '男', name: '男' },
        { id: '女', name: '女' }
      ],
      classList: []
    }
  },
  computed: {
    ...mapGetters(['schoolList', 'courseList'])
  },
  methods: {
    open(item) {
      this.ruleForm = { ...item || {}}
      this.show = true
    },
    changeSchool(val) {
      http._class.list({
        school_id: val
      }).then(res => {
        this.classList = res
      })
    },
    onAdd() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$emit('save', this.ruleForm)
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
